home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / mac / raytrace / dkb / mcprt102.bnh / DKBTrace-Mac / render.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-24  |  15.4 KB  |  468 lines

  1. /*****************************************************************************
  2. *
  3. *                                    render.c
  4. *
  5. *   from DKBTrace (c) 1990  David Buck
  6. *
  7. *  This module implements the main raytracing loop.
  8. *
  9. * This software is freely distributable. The source and/or object code may be
  10. * copied or uploaded to communications services so long as this notice remains
  11. * at the top of each file.  If any changes are made to the program, you must
  12. * clearly indicate in the documentation and in the programs startup message
  13. * who it was who made the changes. The documentation should also describe what
  14. * those changes were. This software may not be included in whole or in
  15. * part into any commercial package without the express written consent of the
  16. * author.  It may, however, be included in other public domain or freely
  17. * distributed software so long as the proper credit for the software is given.
  18. *
  19. * This software is provided as is without any guarantees or warranty. Although
  20. * the author has attempted to find and correct any bugs in the software, he
  21. * is not responsible for any damage caused by the use of the software.  The
  22. * author is under no obligation to provide service, corrections, or upgrades
  23. * to this package.
  24. *
  25. * Despite all the legal stuff above, if you do find bugs, I would like to hear
  26. * about them.  Also, if you have any comments or questions, you may contact me
  27. * at the following address:
  28. *
  29. *     David Buck
  30. *     22C Sonnet Cres.
  31. *     Nepean Ontario
  32. *     Canada, K2H 8W7
  33. *
  34. *  I can also be reached on the following bulleton boards:
  35. *
  36. *     OMX              (613) 731-3419
  37. *     Mystic           (613) 596-4249  or  (613) 596-4772
  38. *
  39. *  Fidonet:   1:163/109.9
  40. *  Internet:  dbuck@ccs.carleton.ca
  41. *  The "You Can Call Me RAY" BBS    (708) 358-5611
  42. *
  43. *  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  44. *
  45. *     The "You Can Call Me RAY" BBS (708) 358-5611
  46. *     The Information Exchange BBS  (708) 945-5575
  47. *
  48. *****************************************************************************/
  49.  
  50. #include "frame.h"
  51. #include "vector.h"
  52. #include "dkbproto.h"
  53.  
  54. extern FILE_HANDLE *Output_File_Handle;
  55. extern char Output_File_Name[FILE_NAME_LENGTH];
  56. extern char OutputFormat;
  57. extern int File_Buffer_Size;
  58. extern unsigned int Options;
  59. extern int Quality;
  60. volatile int Stop_Flag;
  61. extern int First_Line, Last_Line;
  62. extern long Number_Of_Pixels, Number_Of_Rays, Number_Of_Pixels_Supersampled;
  63.  
  64. extern short *hashTable;
  65. extern unsigned short crctab[256];
  66. #define rand3d(a,b) crctab[(int)(hashTable[(int)(hashTable[(int)((a)&0xfff)]^(b))&0xfff])&0xff]
  67.  
  68. FRAME Frame;
  69. RAY *VP_Ray;
  70. int Trace_Level, SuperSampleCount;
  71.  
  72.  
  73. #define MAX_TRACE_LEVEL 5
  74.  
  75. void Output_Line PARAMS((int y, COLOUR **Previous_Line, COLOUR **Current_Line,
  76.   char **Previous_Line_Antialiased_Flags, char **Current_Line_Antialiased_Flags));
  77.  
  78. COLOUR *Previous_Line, *Current_Line;
  79. char *Previous_Line_Antialiased_Flags, *Current_Line_Antialiased_Flags;
  80. RAY Ray;
  81.  
  82. void Create_Ray (ray, width, height, x, y)
  83.    RAY *ray;
  84.    int width, height;
  85.    DBL x, y;
  86.    {
  87.    register DBL X_Scalar, Y_Scalar;
  88.    VECTOR Temp_Vect_1, Temp_Vect_2;
  89.  
  90.    /* Convert the X Coordinate to be a DBL from 0.0 to 1.0 */
  91.    X_Scalar = (x - (DBL) width / 2.0) / (DBL) width;
  92.  
  93.    /* Convert the Y Coordinate to be a DBL from 0.0 to 1.0 */
  94.    Y_Scalar = (( (DBL)(Frame.Screen_Height - 1) - y) -
  95.           (DBL) height / 2.0) / (DBL) height;
  96.  
  97.    VScale (Temp_Vect_1, Frame.View_Point.Up, Y_Scalar);
  98.    VScale (Temp_Vect_2, Frame.View_Point.Right, X_Scalar);
  99.    VAdd (ray->Direction, Temp_Vect_1, Temp_Vect_2);
  100.    VAdd (ray->Direction, ray->Direction, Frame.View_Point.Direction);
  101.    VNormalize (ray->Direction, ray->Direction);
  102.    Initialize_Ray_Containers (ray);
  103.    ray->Quadric_Constants_Cached = FALSE;
  104.    }
  105.  
  106.  
  107. void Supersample (result, x, y, Width, Height)
  108.    COLOUR *result;
  109.    int x, y, Width, Height;
  110.    {
  111.    COLOUR colour;
  112.    register DBL dx, dy, Jitter_X, Jitter_Y;
  113.  
  114.    dx = (DBL) x;
  115.    dy = (DBL) y;
  116.  
  117.    Number_Of_Pixels_Supersampled++;
  118.  
  119.    Make_Colour (result, 0.0, 0.0, 0.0);
  120.  
  121.    Jitter_X = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  122.    Jitter_Y = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  123.    Create_Ray (VP_Ray, Frame.Screen_Width, Frame.Screen_Height,
  124.              dx + Jitter_X, dy + Jitter_Y);
  125.  
  126.    Trace_Level = 0;
  127.    Trace (VP_Ray, &colour);
  128.    Clip_Colour (&colour, &colour);
  129.    Scale_Colour (&colour, &colour, 0.11111111);
  130.    Add_Colour (result, result, &colour);
  131.  
  132.    Jitter_X = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  133.    Jitter_Y = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  134.    Create_Ray (VP_Ray, Width, Height, dx + Jitter_X - 0.3333333,
  135.                                       dy + Jitter_Y - 0.3333333);
  136.    Trace_Level = 0;
  137.    Trace (VP_Ray, &colour);
  138.    Clip_Colour (&colour, &colour);
  139.    Scale_Colour (&colour, &colour, 0.11111111);
  140.    Add_Colour (result, result, &colour);
  141.  
  142.    Jitter_X = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  143.    Jitter_Y = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  144.    Create_Ray (VP_Ray, Width, Height, dx + Jitter_X - 0.3333333,
  145.                                       dy + Jitter_Y);
  146.    Trace_Level = 0;
  147.    Trace (VP_Ray, &colour);
  148.    Clip_Colour (&colour, &colour);
  149.    Scale_Colour (&colour, &colour, 0.11111111);
  150.    Add_Colour (result, result, &colour);
  151.  
  152.    Jitter_X = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  153.    Jitter_Y = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  154.    Create_Ray (VP_Ray, Width, Height, dx + Jitter_X - 0.3333333,
  155.                                       dy + Jitter_Y + 0.3333333);
  156.    Trace_Level = 0;
  157.    Trace (VP_Ray, &colour);
  158.    Clip_Colour (&colour, &colour);
  159.    Scale_Colour (&colour, &colour, 0.11111111);
  160.    Add_Colour (result, result, &colour);
  161.  
  162.    Jitter_X = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  163.    Jitter_Y = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  164.    Create_Ray (VP_Ray, Width, Height, dx + Jitter_X,
  165.                                       dy + Jitter_Y - 0.3333333);
  166.    Trace_Level = 0;
  167.    Trace (VP_Ray, &colour);
  168.    Clip_Colour (&colour, &colour);
  169.    Scale_Colour (&colour, &colour, 0.11111111);
  170.    Add_Colour (result, result, &colour);
  171.  
  172.    Jitter_X = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  173.    Jitter_Y = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  174.    Create_Ray (VP_Ray, Width, Height, dx + Jitter_X,
  175.                                       dy + Jitter_Y + 0.3333333);
  176.    Trace_Level = 0;
  177.    Trace (VP_Ray, &colour);
  178.    Clip_Colour (&colour, &colour);
  179.    Scale_Colour (&colour, &colour, 0.11111111);
  180.    Add_Colour (result, result, &colour);
  181.  
  182.    Jitter_X = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  183.    Jitter_Y = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  184.    Create_Ray (VP_Ray, Width, Height, dx + Jitter_X + 0.3333333,
  185.                                       dy + Jitter_Y - 0.3333333);
  186.    Trace_Level = 0;
  187.    Trace (VP_Ray, &colour);
  188.    Clip_Colour (&colour, &colour);
  189.    Scale_Colour (&colour, &colour, 0.11111111);
  190.    Add_Colour (result, result, &colour);
  191.  
  192.    Jitter_X = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  193.    Jitter_Y = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  194.    Create_Ray (VP_Ray, Width, Height, dx + Jitter_X + 0.3333333,
  195.                                       dy + Jitter_Y);
  196.    Trace_Level = 0;
  197.    Trace (VP_Ray, &colour);
  198.    Clip_Colour (&colour, &colour);
  199.    Scale_Colour (&colour, &colour, 0.11111111);
  200.    Add_Colour (result, result, &colour);
  201.  
  202.    Jitter_X = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  203.    Jitter_Y = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  204.    Create_Ray (VP_Ray, Width, Height, dx + Jitter_X + 0.3333333,
  205.                                       dy + Jitter_Y + 0.3333333);
  206.    Trace_Level = 0;
  207.    Trace (VP_Ray, &colour);
  208.    Clip_Colour (&colour, &colour);
  209.    Scale_Colour (&colour, &colour, 0.11111111);
  210.    Add_Colour (result, result, &colour);
  211.    }
  212.  
  213. void initialize_renderer()
  214.    {
  215.    allocate_lines(&Previous_Line, &Current_Line, &Previous_Line_Antialiased_Flags, &Current_Line_Antialiased_Flags, &Ray);
  216.    }
  217.  
  218. void Read_Rendered_Part()
  219.    {
  220.    int rc, x, line_number;
  221.    char Red, Green, Blue;
  222.  
  223.    while ((rc = Read_Line(Output_File_Handle, Previous_Line, &line_number)) == 1) {
  224.       if (Options & DISPLAY)
  225.          for (x = 0 ; x < Frame.Screen_Width ; x++) {
  226.             Red = (unsigned char) (Previous_Line[x].Red * 255.0);
  227.             Green = (unsigned char) (Previous_Line[x].Green * 255.0);
  228.             Blue = (unsigned char) (Previous_Line[x].Blue * 255.0);
  229.             display_plot (x, line_number, Red, Green, Blue);
  230.             }
  231.       }
  232.  
  233.    First_Line = line_number+1;
  234.  
  235.    if (rc == 0) {
  236.       Close_File(Output_File_Handle);
  237.       if (Open_File (Output_File_Handle, Output_File_Name,
  238.               &Frame.Screen_Width, &Frame.Screen_Height, File_Buffer_Size,
  239.               APPEND_MODE) != 1) {
  240.          fprintf (stderr, "Error opening output file\n");
  241.          exit(1);
  242.          }
  243.       return;
  244.       }
  245.  
  246.    fprintf (stderr, "Error reading aborted data file\n");
  247.    }
  248.  
  249. void Start_Tracing ()
  250.    {
  251.    COLOUR Colour;
  252.    register int x, y;
  253.    unsigned char Red, Green, Blue, Antialias_Center_Flag;
  254.  
  255.    for (y = (Options & ANTIALIAS)?First_Line-1:First_Line; y<Last_Line; y++) {
  256.  
  257.       check_stats(y);
  258.  
  259.       for (x = 0 ; x < Frame.Screen_Width ; x++) {
  260.          Number_Of_Pixels++;
  261.          if (Stop_Flag) {
  262.             close_all();
  263.             exit(0);
  264.             }
  265.  
  266. #ifndef Macintosh
  267.          TEST_ABORT
  268. #endif
  269.  
  270.          Create_Ray (VP_Ray, Frame.Screen_Width, Frame.Screen_Height, (DBL) x, (DBL) y);
  271.          Trace_Level = 0;
  272.          Trace (&Ray, &Colour);
  273.          Clip_Colour (&Colour, &Colour);
  274.  
  275.          Current_Line[x] = Colour;
  276.  
  277.          if (Options & ANTIALIAS) {
  278.             Antialias_Center_Flag = 0;
  279.             Current_Line_Antialiased_Flags[x] = 0;
  280.  
  281.             if (x != 0) {
  282.                if (Colour_Distance (&Current_Line[x-1], &Current_Line[x])
  283.                    >= Frame.Antialias_Threshold) {
  284.                   Antialias_Center_Flag = 1;
  285.                   if (!(Current_Line_Antialiased_Flags[x-1])) {
  286.                      Supersample (&Current_Line[x-1], 
  287.                                   x-1, y, Frame.Screen_Width, Frame.Screen_Height);
  288.                      Current_Line_Antialiased_Flags[x-1] = 1;
  289.                      SuperSampleCount++;
  290.                      }
  291.                   }
  292.                }
  293.  
  294.             if (y != First_Line-1) {
  295.                if (Colour_Distance (&Previous_Line[x], &Current_Line[x])
  296.                     >= Frame.Antialias_Threshold) {
  297.                   Antialias_Center_Flag = 1;
  298.                   if (!(Previous_Line_Antialiased_Flags[x])) {
  299.                      Supersample (&Previous_Line[x],
  300.                                   x, y-1, Frame.Screen_Width, Frame.Screen_Height);
  301.                      Previous_Line_Antialiased_Flags[x] = 1;
  302.                      SuperSampleCount++;
  303.                      }
  304.                   }
  305.                }
  306.  
  307.             if (Antialias_Center_Flag) {
  308.                Supersample (&Current_Line[x],
  309.                             x, y, Frame.Screen_Width, Frame.Screen_Height);
  310.                Current_Line_Antialiased_Flags[x] = 1;
  311.                Colour = Current_Line[x];
  312.                SuperSampleCount++;
  313.                }
  314.             }
  315.  
  316.          if (y != First_Line-1) {
  317.             Red = (unsigned char) (Colour.Red * 255.0);
  318.             Green = (unsigned char) (Colour.Green * 255.0);
  319.             Blue = (unsigned char) (Colour.Blue * 255.0);
  320.  
  321.             if (Options & DISPLAY)
  322.                display_plot (x, y, Red, Green, Blue);
  323.             }
  324.          }
  325.       Output_Line(y, &Previous_Line, &Current_Line, &Previous_Line_Antialiased_Flags, &Current_Line_Antialiased_Flags);
  326.       }
  327.  
  328.    if (Options & DISKWRITE) {
  329.       Write_Line (Output_File_Handle, Previous_Line, Last_Line - 1);
  330.       }
  331.    }
  332.  
  333. void check_stats(y)
  334.    register int y;
  335.    {
  336.       if (Options & VERBOSE)
  337.          printf ("Line %3d", y);
  338.  
  339.       if (Options & ANTIALIAS)
  340.          SuperSampleCount = 0;
  341.    }
  342.  
  343. void  allocate_lines(Previous_Line, Current_Line, Previous_Line_Antialiased_Flags, Current_Line_Antialiased_Flags, Ray)
  344.    COLOUR **Previous_Line, **Current_Line;
  345.    char **Previous_Line_Antialiased_Flags, **Current_Line_Antialiased_Flags;
  346.    RAY *Ray;
  347.    {
  348.    register int i;
  349.  
  350.    VP_Ray = Ray;
  351.  
  352.    *Previous_Line = (COLOUR *) malloc (sizeof (COLOUR)*(Frame.Screen_Width + 1));
  353.    *Current_Line = (COLOUR *) malloc (sizeof (COLOUR)*(Frame.Screen_Width + 1));
  354.  
  355.    for (i = 0 ; i <= Frame.Screen_Width ; i++) {
  356.       (*Previous_Line)[i].Red = 0.0;
  357.       (*Previous_Line)[i].Green = 0.0;
  358.       (*Previous_Line)[i].Blue = 0.0;
  359.  
  360.       (*Current_Line)[i].Red = 0.0;
  361.       (*Current_Line)[i].Green = 0.0;
  362.       (*Current_Line)[i].Blue = 0.0;
  363.       }
  364.  
  365.    if (Options & ANTIALIAS) {
  366.       *Previous_Line_Antialiased_Flags =
  367.           (char *) malloc (sizeof (char)*(Frame.Screen_Width + 1));
  368.       *Current_Line_Antialiased_Flags =
  369.           (char *)  malloc (sizeof (char)*(Frame.Screen_Width + 1));
  370.  
  371.       for (i = 0 ; i <= Frame.Screen_Width ; i++) {
  372.          (*Previous_Line_Antialiased_Flags)[i] = 0;
  373.          (*Current_Line_Antialiased_Flags)[i] = 0;
  374.          }
  375.    }
  376.  
  377.    Ray->Initial = Frame.View_Point.Location;
  378.    return;
  379.    }
  380.  
  381. void Output_Line (y, Previous_Line, Current_Line, Previous_Line_Antialiased_Flags, Current_Line_Antialiased_Flags)
  382.    register int y;
  383.    COLOUR **Previous_Line, **Current_Line;
  384.    char **Previous_Line_Antialiased_Flags, **Current_Line_Antialiased_Flags;
  385.    {
  386.       COLOUR *Temp_Colour_Ptr;
  387.       char *Temp_Char_Ptr;
  388.  
  389.       if (Options & DISKWRITE)
  390.          if (y > First_Line) {
  391.             Write_Line (Output_File_Handle, *Previous_Line, y-1);
  392.          }
  393.  
  394.       if (Options & VERBOSE)
  395.          if (Options & ANTIALIAS)
  396.             printf (" supersampled %d times\n", SuperSampleCount);
  397.          else
  398.             putchar ('\n');
  399.  
  400.       Temp_Colour_Ptr = *Previous_Line;
  401.       *Previous_Line = *Current_Line;
  402.       *Current_Line = Temp_Colour_Ptr;
  403.  
  404.       Temp_Char_Ptr = *Previous_Line_Antialiased_Flags;
  405.       *Previous_Line_Antialiased_Flags = *Current_Line_Antialiased_Flags;
  406.       *Current_Line_Antialiased_Flags = Temp_Char_Ptr;
  407.  
  408.       return;
  409.    }
  410.  
  411. void Trace (Ray, Colour)
  412.    RAY *Ray;
  413.    COLOUR *Colour;
  414.    {
  415.    OBJECT *Object;
  416.    INTERSECTION *Local_Intersection, *New_Intersection;
  417.    register int Intersection_Found;
  418.  
  419. #ifdef Macintosh
  420.    TEST_ABORT
  421. #endif
  422.    
  423.    Number_Of_Rays++;
  424.    Make_Colour (Colour, 0.0, 0.0, 0.0);
  425.  
  426.    Intersection_Found = FALSE;
  427.    Local_Intersection = NULL;
  428.  
  429.    if (Trace_Level > MAX_TRACE_LEVEL) {
  430.       return;
  431.       }
  432.  
  433.    if (Frame.Fog_Distance == 0.0) {
  434.       Make_Colour (Colour, 0.0, 0.0, 0.0);
  435.       }
  436.    else
  437.       *Colour = Frame.Fog_Colour;
  438.  
  439.    if (Options & DEBUGGING)
  440.       printf ("Calculating intersections level %d\n", Trace_Level);
  441.  
  442.     /* What objects does this ray intersect? */
  443.    for (Object = Frame.Objects ; 
  444.         Object != NULL ;
  445.         Object = Object -> Next_Object) {
  446.  
  447.       if (New_Intersection = Intersection (Object, Ray)) {
  448.          if (Intersection_Found) {
  449.             if (Local_Intersection -> Depth > New_Intersection -> Depth) {
  450.                free (Local_Intersection);
  451.                Local_Intersection = New_Intersection;
  452.                }
  453.             else
  454.                free (New_Intersection);
  455.             }
  456.          else
  457.             Local_Intersection = New_Intersection;
  458.  
  459.          Intersection_Found = TRUE;
  460.          }
  461.       }
  462.  
  463.    if (Intersection_Found) {
  464.       Determine_Surface_Colour (Local_Intersection, Colour, Ray, FALSE);
  465.       free (Local_Intersection);
  466.       }
  467.    }
  468.